var notice = {
    init: function() {
        var that = this;


        that.dateInit();
        that.getList(0, 0);



        //close
        $("body").on('click', '#noticeClose,#noticeCancel', function() {
            $("#issueTmp").remove();
        });

        //issue
        $("body").on('click', '#noticeSure', function() {
            var title = $.trim($("#noticeTitle").val()),
                con = $.trim($("#noticeCon").val());
            $.ajax({
            	url:COMMONURL + "api/Notice/Add?token=" + $.cookie('token'),
            	type:"post",
            	data:{
                 title:title,
                 content:con
            	},
            	dataType:"json"
            }).done(function(redata){});
        });

        //查询
        var $release_time1 = $("#release_time1"),
            $release_time2 = $("#release_time2");

        $("#noticeQqueryBtn").click(function() {
            var release_time1 = new Date($('#release_time1').val()).getTime()/1000,
                release_time2 = new Date($('#release_time2').val()).getTime()/1000;
            that.getList(release_time1, release_time2);
        });
    },
    getList: function(release_time1, release_time2) {
        $.ajax({
            url: COMMONURL + "api/CompanyNotice/GetPageList?token=" + $.cookie('token'),
            type: "post",
            data: {
                release_time1: release_time1,
                release_time2: release_time2,
                page: 1,
                pageSize: 20
            },
            dataType: "json"
        }).done(function(redata) {
            if (redata.recode == '0000') {
                var compiled = _.template($("#notice_cont").html());
                var odata = redata.mod;
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].start_bid_time = that.formatTime(odata[i].start_bid_time);
                    odata[i].end_bid_time = that.formatTime(odata[i].end_bid_time);
                }
                $("#noticeViewList").html(compiled(odata.Items));

                 $(".jLook").click(function(){

                        var $noticeIssue = $("#noticeIssue"),
                            $issueTpl = $("#issueTpl");
                        var winHeight = $(document).height();
                        var id = $(this).attr("data-id");
                     $.ajax({
                            url: COMMONURL + "api/notice/get?token=" + $.cookie('token'),
                            type: "get",
                            data: {
                                id: id
                               
                            },
                            dataType: "json"
                        }).done(function(redata) {
                            if (redata.recode == '0000') {
                                var compiled = _.template($issueTpl.html());
                                 $('body').append(compiled(redata.mod));
                                $("#issueTmp #issueMask").css('height', winHeight);
                            }else{
                                 alert(data.resmessage);
                            }
                        });
                    
                })




            }
        });
    },
    dateInit: function() {
        $('.datepicker').datetimepicker({
            language: 'zh-CN',
            weekStart: 1,
            todayBtn: 1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            format: 'yyyy-mm-dd hh:ii'
        });
    },
    formatTime: function(nS) {
           if(!nS) return null;
            var date = new Date(nS*1000);
            var year = date.getFullYear();
            var month = date.getMonth()+1;
            var d = date.getDate();
            var hours = date.getHours();
            var minutes = date.getMinutes();
            var seconds = date.getSeconds();
            return year+'-'+month+'-'+d+' '+hours+':'+minutes;
       // Date.prototype.dateFormat = function(fmt) {
        //    var o = {
        //        "M+": this.getMonth() + 1, //月份         
        //        "d+": this.getDate(), //日         
         //       "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时         
         //       "H+": this.getHours(), //小时         
         //       "m+": this.getMinutes(), //分         
         //       "s+": this.getSeconds(), //秒         
         //       "q+": Math.floor((this.getMonth() + 3) / 3), //季度         
          //      "S": this.getMilliseconds() //毫秒         
         //   };
         //   var week = {
         //       "0": "\u65e5",
         //       "1": "\u4e00",
          //      "2": "\u4e8c",
          //      "3": "\u4e09",
          //      "4": "\u56db",
          //      "5": "\u4e94",
          //      "6": "\u516d"
          //  };
          //  if (/(y+)/.test(fmt)) {
          //      fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
           // }
           // if (/(E+)/.test(fmt)) {
           //     fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]);
           // }
           // for (var k in o) {
           //     if (new RegExp("(" + k + ")").test(fmt)) {
            //        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            //    }
            //}
            //return fmt;
       // }
        //return new Date(parseInt(nS) * 1000).dateFormat("yyyy-MM-dd HH:mm");
    }
}


notice.init();
